package leetCode.sample;

import org.junit.Test;

/**
 * 正则匹配
 */
public class RegularMatchSolution {

    @Test
    public void test() {
        System.out.println(isMatch("aa", "a"));
        System.out.println(isMatch("aa", "aa"));
        System.out.println(isMatch("aab", "aa"));
        System.out.println(isMatch("aab", "aa."));
        System.out.println(isMatch("aa", "aa.*"));
        System.out.println("aab".substring(3));
    }


    public boolean isMatch(String s, String p) {
        if (p.isEmpty()) {
            return s.isEmpty();
        }
        boolean firstMatch = (!s.isEmpty() && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.'));
        if (p.length() > 1 && p.charAt(1) == '*') {
            return isMatch(s, p.substring(2)) || (firstMatch && isMatch(s.substring(1), p));
        } else {
            return firstMatch && isMatch(s.substring(1), p.substring(1));
        }
    }
}
